#!/usr/bin/python
# -*- coding: utf-8 -*-
#Author: Onur ASLAN
#Mail :slnnronur@gmail.com 



from PyQt4 import QtCore
from PyQt4 import QtGui
from PyQt4.phonon import Phonon
from PyQt4 import phonon
from ui_mainwindow import Ui_MainWindow

import ajans
import downloadOldAgency
import playListPlay
import ajansComments

import sys, os
import urllib2
import getpass
import icons_rc

		
class MainWindow(QtGui.QMainWindow, Ui_MainWindow):
	def __init__(self):
		QtGui.QMainWindow.__init__(self)
		self.setupUi(self)
		global a

		self.actionOynat.setEnabled(False)
		self.actionDuraklat.setEnabled(False)
		self.pushButton_5.setEnabled(False)
		
		self.toolBar.addWidget(self.seekSlider)
		self.toolBar.addWidget(self.volumeSlider)


			
		QtCore.QObject.connect(self.listWidget, QtCore.SIGNAL("itemDoubleClicked(QListWidgetItem*)"), self.listenPodC)
		QtCore.QObject.connect(self.listWidget_2, QtCore.SIGNAL("itemDoubleClicked(QListWidgetItem*)"), self.listenDownloaded)
		QtCore.QObject.connect(self.listListen, QtCore.SIGNAL("clicked()"), self.listenPodC)
		QtCore.QObject.connect(self.guncelDinle, QtCore.SIGNAL("clicked()"), self.listenNow)
		QtCore.QObject.connect(self.guncelMp3, QtCore.SIGNAL("clicked()"), self.mp3Download)		
		QtCore.QObject.connect(self.guncelOgg, QtCore.SIGNAL("clicked()"), self.oggDownload)		
		QtCore.QObject.connect(self.actionOynat, QtCore.SIGNAL("triggered(bool)"), self.play)
		QtCore.QObject.connect(self.actionDuraklat, QtCore.SIGNAL("triggered(bool)"), self.pause)
		QtCore.QObject.connect(self.listOgg, QtCore.SIGNAL("clicked()"), self.listOggDown)		
		QtCore.QObject.connect(self.listMp3, QtCore.SIGNAL("clicked()"), self.listMp3Down)				
		QtCore.QObject.connect(self.pushButton_5, QtCore.SIGNAL("clicked()"), self.listenDownloaded)
		
		QtCore.QObject.connect(self.actionGuncelAjans, QtCore.SIGNAL("triggered(bool)"), self.guncelAjans)
		QtCore.QObject.connect(self.oldAgency_2, QtCore.SIGNAL("triggered(bool)"), self.goAgencyList)
		QtCore.QObject.connect(self.pushButton_6, QtCore.SIGNAL("triggered(bool)"), self.goAbout)
		QtCore.QObject.connect(self.actionAyarlar, QtCore.SIGNAL("triggered(bool)"), self.goSettings)
		QtCore.QObject.connect(self.closeButton_2, QtCore.SIGNAL("triggered(bool)"), self.closeProg)		
		QtCore.QObject.connect(self.actionIndirilenler, QtCore.SIGNAL("triggered(bool)"), self.goDownloadList)				
		QtCore.QObject.connect(self.pushButton_2, QtCore.SIGNAL("clicked()"), self.saveSetting)
		QtCore.QObject.connect(self.goster, QtCore.SIGNAL("clicked()"), self.showDir)
		QtCore.QObject.connect(self.trayIcon, QtCore.SIGNAL("active(bool)"), self.iconActivated)
		
		
		kontrolSett = str(os.path.exists("/home/"+str(getpass.getuser())+"/.sumru-settings"))
		if kontrolSett == "False":
			print "Ayar dosyası oluşturuluyor..."
			self.lineEdit.setText("/home/"+str(getpass.getuser())+"/")
			idFile = open("/home/"+str(getpass.getuser())+"/.sumru-settings", "w")
		 	idFile.write("/home/"+str(getpass.getuser())+"/")
		 	idFile.close() 
		 	 
		if kontrolSett == "True":
		 	dosyaa = open("/home/"+str(getpass.getuser())+"/.sumru-settings")
		 	for satir in dosyaa:
			  	a = satir
			self.lineEdit.setText(unicode(a)) 	
		 	dosyaa.readline() 	
	 			
			
		image_urlo = "http://www.ozgurlukicin.com/media/uploads/haberler/podcast-acilis.png"
		opener15 = urllib2.build_opener()
		page15 = opener15.open(image_urlo)
		down_imageo = page15.read()		
		fout = open("/tmp/podcast-acilis.png", "wb")
		fout.write(down_imageo)
		fout.close()
		image = "/tmp/podcast-acilis.png"

      		nu = ajans.no1.split("/")


   		
		self.label.setText(unicode("<b>"+ajans.baslik+"</b><p>"+""+ajans.aciklama+"<p>"+""+ajans.sure+"<p>"+""+ajans.sunucu+"<p>"+""+ajans.zaman+"<p>"+"<b>Yorumlar: </b>"+ajansComments.yorumlar[0].split('"><span class="bold">')[1]))
	

		icon1 = QtGui.QIcon()
		icon1.addPixmap(QtGui.QPixmap(image), QtGui.QIcon.Normal, QtGui.QIcon.Off)
		
		self.guncelGorsel.setPixmap(QtGui.QPixmap(":/icons/podcast.png"))
		
		item = QtGui.QListWidgetItem(self.listWidget)
		item.setIcon(icon1)
		item.setText(ajans.titleNow[1].replace("&#39;", "'")+"\n"+unicode(ajans.descrip[1].replace("&#39;", "'"))+"\n"+ajans.author[0]+"  Uzunluk: "+ajans.uzunluk[0]+" Yorumlar: "+ajansComments.yorumlar[0].split('"><span class="bold">')[1])
		
		item1 = QtGui.QListWidgetItem(self.listWidget)
		item1.setIcon(icon1)		
		item1.setText(ajans.titleNow[2].replace("&#39;", "'")+"\n"+unicode(ajans.descrip[2].replace("&#39;", "'"))+"\n"+ajans.author[1]+"  Uzunluk: "+ajans.uzunluk[1]+" Yorumlar: "+ajansComments.yorumlar[1].split('"><span class="bold">')[1])
		
		item2 = QtGui.QListWidgetItem(self.listWidget)
		item2.setIcon(icon1)		
		item2.setText(ajans.titleNow[3].replace("&#39;", "'")+"\n"+unicode(ajans.descrip[3].replace("&#39;", "'"))+"\n"+ajans.author[2]+"  Uzunluk: "+ajans.uzunluk[2]+" Yorumlar: "+ajansComments.yorumlar[2].split('"><span class="bold">')[1])
		
		item3 = QtGui.QListWidgetItem(self.listWidget)
		item3.setIcon(icon1)		
		item3.setText(ajans.titleNow[4].replace("&#39;", "'")+"\n"+unicode(ajans.descrip[4].replace("&#39;", "'"))+"\n"+ajans.author[3]+"  Uzunluk: "+ajans.uzunluk[3]+" Yorumlar: "+ajansComments.yorumlar[3].split('"><span class="bold">')[1])

		item4 = QtGui.QListWidgetItem(self.listWidget)
		item4.setIcon(icon1)		
		item4.setText(ajans.titleNow[5].replace("&#39;", "'")+"\n"+unicode(ajans.descrip[5].replace("&#39;", "'"))+"\n"+ajans.author[4]+"  Uzunluk: "+ajans.uzunluk[4]+" Yorumlar: "+ajansComments.yorumlar[4].split('"><span class="bold">')[1])
		
		item5 = QtGui.QListWidgetItem(self.listWidget)
		item5.setIcon(icon1)		
		item5.setText(ajans.titleNow[6].replace("&#39;", "'")+"\n"+unicode(ajans.descrip[6].replace("&#39;", "'"))+"\n"+ajans.author[5]+"  Uzunluk: "+ajans.uzunluk[5]+" Yorumlar: "+ajansComments.yorumlar[5].split('"><span class="bold">')[1])
		
		item6 = QtGui.QListWidgetItem(self.listWidget)
		item6.setIcon(icon1)		
		item6.setText(ajans.titleNow[7].replace("&#39;", "'")+"\n"+unicode(ajans.descrip[7].replace("&#39;", "'"))+"\n"+ajans.author[6]+"  Uzunluk: "+ajans.uzunluk[6]+" Yorumlar: "+ajansComments.yorumlar[6].split('"><span class="bold">')[1])
		
		item7 = QtGui.QListWidgetItem(self.listWidget)
		item7.setIcon(icon1)		
		item7.setText(ajans.titleNow[8].replace("&#39;", "'")+"\n"+unicode(ajans.descrip[8].replace("&#39;", "'"))+"\n"+ajans.author[7]+"  Uzunluk: "+ajans.uzunluk[7]+" Yorumlar: "+ajansComments.yorumlar[7].split('"><span class="bold">')[1])
		
		item8 = QtGui.QListWidgetItem(self.listWidget)
		item8.setIcon(icon1)		
		item8.setText(ajans.titleNow[9].replace("&#39;", "'")+"\n"+unicode(ajans.descrip[9].replace("&#39;", "'"))+"\n"+ajans.author[8]+"  Uzunluk: "+ajans.uzunluk[8]+" Yorumlar: "+ajansComments.yorumlar[9].split('"><span class="bold">')[1])
		
		item9 = QtGui.QListWidgetItem(self.listWidget)
		item9.setIcon(icon1)		
		item9.setText(ajans.titleNow[10].replace("&#39;", "'")+"\n"+unicode(ajans.descrip[10].replace("&#39;", "'"))+"\n"+ajans.author[9]+"  Uzunluk: "+ajans.uzunluk[9]+" Yorumlar: "+ajansComments.yorumlar[9].split('"><span class="bold">')[1])

		item10 = QtGui.QListWidgetItem(self.listWidget)
		item10.setIcon(icon1)		
		item10.setText(ajans.titleNow[11].replace("&#39;", "'")+"\n"+unicode(ajans.descrip[11].replace("&#39;", "'"))+"\n"+ajans.author[10]+"  Uzunluk: "+ajans.uzunluk[10]+" Yorumlar: "+ajansComments.yorumlar[10].split('"><span class="bold">')[1])		

	def iconActivated(self, reason):
		if reason in (QtGui.QSystemTrayIcon.Trigger, QtGui.QSystemTrayIcon.DoubleClick):
			if self.isVisible():
				self.setVisible(False) 
			else:
				self.setVisible(True)


	def saveSetting(self):
		kontrolSett = str(os.path.exists("/home/"+str(getpass.getuser())+"/.sumru-settings"))
		if kontrolSett == "False":
			self.lineEdit.setText("/home/"+str(getpass.getuser())+"/")
			print "Ayar dosyası oluşturuluyor..."
			idFile = open("/home/"+str(getpass.getuser())+"/.sumru-settings", "w")
		 	idFile.write("/home/"+str(getpass.getuser())+"/")
		 	idFile.close() 
		 	 
		if kontrolSett == "True":
			print "Ayar dosyası oluşturuluyor..."
			idFile = open("/home/"+str(getpass.getuser())+"/.sumru-settings", "w")
		 	idFile.write(unicode(self.lineEdit.text()))
		 	idFile.close() 
		 	print "Ayarlar kaydedildi."
		
		self.durum.setText("Ayarlar Kaydedildi")	  	
	
	
	def play(self):
		self.videoPlayer.play()
		self.actionOynat.setEnabled(False)
		self.actionDuraklat.setEnabled(True)
		
	def pause(self):
		self.videoPlayer.pause()
		self.actionOynat.setEnabled(True)
		self.actionDuraklat.setEnabled(False)
		
	def listOggDown(self):
		downloadOldAgency.OGGDown(self)
			
	def listMp3Down(self):
		downloadOldAgency.MP3Down(self)	


	def oggDownload(self):
		self.pushButton_5.setEnabled(True)
		icon1 = QtGui.QIcon()
		icon1.addPixmap(QtGui.QPixmap("/tmp/podcast-acilis.png"), QtGui.QIcon.Normal, QtGui.QIcon.Off)		

		self.stackedWidget.setCurrentIndex(2)

		adres = ajans.adresOGG[0]
		isim = ajans.adresOGG[0].split("/podcasts")[1]
		print isim
		yol = self.lineEdit.text()+"/"	
		item = QtGui.QListWidgetItem(self.listWidget_2)
		item.setIcon(icon1)
		item.setText(unicode(yol+isim))


		dosya = urllib2.urlopen(adres)
		indir = open(yol+isim, "w")
		indir.write(dosya.read())
		dosya.close()

	
	def mp3Download(self):
		self.pushButton_5.setEnabled(True)
		icon1 = QtGui.QIcon()
		icon1.addPixmap(QtGui.QPixmap("/tmp/podcast-acilis.png"), QtGui.QIcon.Normal, QtGui.QIcon.Off)		

		self.stackedWidget.setCurrentIndex(2)

		adres = ajans.adresMP3[0]
		isim = ajans.adresMP3[0].split("/podcasts")[1]
		print isim
		yol = self.lineEdit.text()+"/"	
		item = QtGui.QListWidgetItem(self.listWidget_2)
		item.setIcon(icon1)
		item.setText(unicode(yol+isim))


		dosya = urllib2.urlopen(adres)
		indir = open(yol+isim, "w")
		indir.write(dosya.read())
		dosya.close()

	def listenDownloaded(self):
		self.pushButton_5.setEnabled(True)
		self.actionOynat.setEnabled(False)
		self.actionDuraklat.setEnabled(True)
		url = unicode(self.listWidget_2.currentItem().text())
		self.videoPlayer.load(Phonon.MediaSource(url))
		self.videoPlayer.play()			

		
	def listenNow(self):
		self.actionOynat.setEnabled(False) 
		self.actionDuraklat.setEnabled(True)		
		nu = ajans.no1.split("/")
		adres =  ajans.adresMP3[0]
		self.durum.setText(unicode("Dinleniliyor: Bölüm ")+nu[0])
		self.videoPlayer.load(Phonon.MediaSource(adres))
		self.videoPlayer.play()		
			
	def listenPodC(self):	
		playListPlay.listenList(self)
		
	def guncelAjans(self):
		self.stackedWidget.setCurrentIndex(0)
					
	def goAgencyList(self):
		self.stackedWidget.setCurrentIndex(1)

	def goAbout(self):
		self.stackedWidget.setCurrentIndex(4)

	def goDownloadList(self):
		self.stackedWidget.setCurrentIndex(2)	
			
	def goSettings(self):
		self.stackedWidget.setCurrentIndex(3)				

	def closeProg(self):
		sys.exit()	
		
 	def showDir(self):
		konum = QtGui.QFileDialog.getExistingDirectory()
		self.lineEdit.setText(unicode(konum))
				       		
app = QtGui.QApplication(sys.argv)
app.setApplicationName("sumru")
window = MainWindow()
window.show()
sys.exit(app.exec_())